﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using Microsoft.VisualBasic.Devices;
using System.IO;
namespace DXWindowsApplication1.MainForm
{
    public class MainExplorer {

        public virtual void Copy(ListView listview, DevExpress.XtraEditors.TextEdit textEdit, 
                                ref string srcdirectory, ref string srcfile,
                                ref string itemname, ref bool flagcopy, ref bool flagdir)
        {
            flagcopy = true;
            ListViewItem item = new ListViewItem();

            try
            {
                if (listview.Focused)
                {
                    for (int i = 0; i < listview.SelectedItems.Count; i++)
                    {
                        item = listview.SelectedItems[i];
                        if (item.SubItems[1].Text.Trim().Equals("Folder"))
                        {
                            flagdir = true;
                            itemname = item.Name;
                            srcdirectory = textEdit.Text + itemname;
                        }
                        else
                        {
                            flagdir = false;
                            itemname = item.Name;
                            srcfile = textEdit.Text + itemname;
                        }
                    }
                }
               
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            } 
        }

        public virtual void Cut(ListView listview, DevExpress.XtraEditors.TextEdit textEdit,
                               ref string srcdirectory, ref string srcfile,
                               ref string itemname, ref bool flagcopy, ref bool flagdir)
        {
            flagcopy = false;
            ListViewItem item = new ListViewItem();
            item.ForeColor = System.Drawing.Color.LightGray;

            try
            {
                if (listview.Focused)
                {
                    item = listview.FocusedItem;
                    if (item.SubItems[1].Text.Trim().Equals("Folder"))
                    {
                        flagdir = true;
                        itemname = item.Name;
                        srcdirectory = textEdit.Text + itemname;
                    }
                    else
                    {
                        flagdir = false;
                        itemname = item.Name;
                        srcfile = textEdit.Text + itemname;
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void Paste(DevExpress.XtraEditors.TextEdit textEdit,
                                    ref string srcDir, ref string destDir,
                                    ref string srcFile, ref string destFile,
                                    ref string itemname, ref bool flagcopy, ref bool flagdir) {

                                        
                                        try {
                                            destDir = textEdit.Text;
                                            destFile = destDir + itemname;

                                            if (flagcopy)
                                            {
                                                if (flagdir)
                                                {
                                                    FileSystem.CopyDirectory(srcDir, destDir);
                                                }
                                                else
                                                {
                                                    FileSystem.CopyFile(srcFile, destFile);
                                                }
                                            }
                                            else {
                                                if (flagdir)
                                                {
                                                    FileSystem.MoveDirectory(srcDir, destDir);
                                                }
                                                else
                                                {
                                                    FileSystem.MoveFile(srcFile, destFile);
                                                }
                                            }
                                        }
                                        catch (Exception ex) {
                                            MessageBox.Show(ex.Message);
                                        }
        }

        public virtual void Delete(ListViewItem item, ListView listview) {

            string path = item.SubItems[4].Text;
            if (item.SubItems[1].Text.Equals("Folder"))
            {
                DirectoryInfo dirinfo = new DirectoryInfo(path);
                if (!dirinfo.Exists)
                {
                    MessageBox.Show("Thu muc khong ton tai", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return;
                }
                else
                {
                    DialogResult dialresult = MessageBox.Show("Ban co muon xoa thu muc " + item.Name + " nay khong ?",
                                                              "Thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dialresult == DialogResult.Yes)
                    {
                        dirinfo.Delete(true);
                    }
                    else {
                        return;
                    }
                }
            }
            else {
                FileInfo fileinfo = new FileInfo(path);
                if (!fileinfo.Exists)
                {
                    MessageBox.Show("Thu muc khong ton tai", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else {
                    DialogResult dialresult = MessageBox.Show("Ban co muon xoa file " + item.Name + " nay khong ?",
                                                                "Thong bao", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (dialresult == DialogResult.Yes)
                    {
                        fileinfo.Delete();
                    }
                    else {
                        return;
                    }
                }
            }
        }

        public void CopyToFolder(ListView listview)
        {
            try
            {
                if (listview.SelectedItems.Count == 0)
                {
                    MessageBox.Show("Chua co doi tuong nao duoc chon", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    FolderBrowserDialog folderbrwdiag = new FolderBrowserDialog();
                    folderbrwdiag.ShowDialog();

                    for (int i = 0; i < listview.SelectedItems.Count; i++)
                    {
                        string path = listview.SelectedItems[i].SubItems[4].Text;
                        if (Directory.Exists(path))
                        {
                            Computer com = new Computer();
                            com.FileSystem.CopyDirectory(path, folderbrwdiag.SelectedPath + "\\" + Path.GetFileName(path),true);
                            
                        }
                        else
                        {
                            FileSystem.CopyFile(path, folderbrwdiag.SelectedPath + "\\" + Path.GetFileName(path));
                        }
                    }
                }
            }
            catch { MessageBox.Show("Khong the copy thu muc nay"); }
        }

        public virtual void MoveToFolder(ListView listview) {

            try {
                if (listview.SelectedItems.Count == 0)
                {
                    MessageBox.Show("Chua co doi tuong nao duoc chon", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else {
                    FolderBrowserDialog folderdialg = new FolderBrowserDialog(); folderdialg.ShowDialog();
                    for (int i = 0; i < listview.SelectedItems.Count; i++) {

                        string path = listview.SelectedItems[i].SubItems[4].Text;
                        if (Directory.Exists(path))
                        {
                            Computer com = new Computer();
                            com.FileSystem.CopyDirectory(path, folderdialg.SelectedPath + "\\" + Path.GetFileName(path),true);
                        }
                        else 
                        { 
                                
                        }
                    }
                }
            }
            catch {}

        } 
    }
}
